/**
 * Created by guodong on 2017/10/30.
 */
'use strict';
angular.module('app')
    .controller('sensitiveWordController', [ '$rootScope', '$scope', '$http', '$state','$timeout',
        function($rootScope, $scope, $http, $state, $timeout) {
            $scope.title = '敏感词配置';
            $scope.searchParams = { };
            $scope.loading = false;
            $scope.param={};
            $scope.search = function () {
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name+'/sensitiveManage/sensitiveWord/list',
                    data: $scope.searchParams
                }).then(function(result) {
                    console.log(result);
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

            $scope.searchRe = function () {
                $scope.loading = true;
                $scope.searchParams.pageNum = 1;
                $.ajax({
                    url : $scope.app.project_name+'/sensitiveManage/sensitiveWord/list',
                    data: $scope.searchParams
                }).then(function(result) {

                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }


    $scope.uploadFile = function (){
         var fileObj = document.getElementById("csv").files[0]; // 获取文件对象
         if(fileObj){
             console.log(fileObj);
              // FormData 对象
                  var form = new FormData();
                  form.append("file", fileObj);// 文件对象
                  $.ajax({
                      url : $scope.app.project_name+'/sensitiveManage/sensitiveWord/addList',
                       type: 'POST',
                        data: form,
                        async: false,
                        cache: false,
                        contentType: false,
                        processData: false
                  }).then(function(result) {
                      console.log(result);
                      $scope.loading = false;
                      if (result.httpCode == 200) {
                           $scope.alertModal({"content":"操作成功,成功添加"+result.successfulQuantity+"个敏感词"});
                            $scope.search();
                      } else {
                      $scope.msg = result.msg;

                      }
//                      $scope.$apply();
                  });
         }else{
             $scope.alertModal({"content":"未选择文件"});
         }
     }

        //导出模板
                 $scope.fileDown = function () {
                     var name='SensitiveWords.csv';
                      window.location.href = $scope.app.project_name + "/templet/download?fileName=" + name;
                 }
            $scope.importRedis = function () {
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name+'/sensitiveManage/sensitiveWord/importRedis',
                }).then(function(result) {
                    console.log(result);
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                         $scope.alertModal({"content":"操作成功"});
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

            $scope.search();

            $scope.clearSearch = function() {
                $scope.searchParams= { };
                $scope.search();
            }

            // 翻页
            $scope.pagination = function (pageNo) {
                $scope.searchParams.pageNum=pageNo;
                $scope.search();
            };

            $scope.oper = function (id,$event) {

                $.ajax({
                    url : $scope.app.project_name+'/sensitiveManage/sensitiveWord/banEnable',
                    data: {id:id,enable:$event.target.checked?'Y':'N'}
                }).then(function(result) {
                    $scope.loading = false;
                    $scope.alertModal({"content":"操作成功"});
                    if (result.httpCode != 200) {
                        $scope.alertModal(result.msg);
                    }
                    $scope.search();
                });
            }

            // 平台名称鼠标移入的动作
            $scope.introduction = [];
            $scope.preview = function(introduction){
                var showDiv = document.getElementById('showDiv');
                showDiv.style.left = event.clientX;
                showDiv.style.top = event.clientY;
                showDiv.style.display = 'block';
                showDiv.innerHTML = introduction;
            }
            // 平台名称鼠标移出的动作
            $scope.outHide = function(){
                var showDiv = document.getElementById('showDiv');
                showDiv.style.display = 'none';
                showDiv.innerHTML = '';
            }

        } ]);
